package com.ztjc.day03_listview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ztjc.day03_listview.R;
import com.ztjc.day03_listview.domain.Person;

import java.util.List;

/**
 * Created by pro on 15/10/8.
 */
public class FriendAdapter extends BaseAdapter {

    private Context context;
    private List<Person> persons;

    public FriendAdapter(Context context, List<Person> persons) {
        this.context = context;
        this.persons = persons;
    }

    @Override
    public int getCount() {
        return persons.size();
    }

    @Override
    public Object getItem(int position) {
        return persons.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Person person = persons.get(position);

        ViewHolder viewHolder = null;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.list_item, null);
            viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.ivIcon.setImageResource(person.getIcon());
        viewHolder.tvName.setText(person.getName());

        return convertView;
    }

    class ViewHolder {
        ImageView ivIcon;
        TextView tvName;
    }
}
